package com.nf.health.app.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

/**
 * 矩形块进度条
 * @author fei
 *
 */
public class RectangleProgressView extends View{

	/**
	 * 方块背景色
	 */
	private Paint bkPaint;
	/**
	 * 方块填充色
	 */
	private Paint paint;
	private float pbHeight;
	private float top;
	private float bottom;
	private float unitSize;
	private float dividerHeight=3;
	private float left = 0f;
	private float right = 0f;
	private int measureWidth;
	private Paint boderPaint;
	public RectangleProgressView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public RectangleProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		init();
	}

	public RectangleProgressView(Context context) {
		super(context);
		
		init();
	}

	private void init() {
		bkPaint = new Paint();
		bkPaint.setColor(Color.parseColor("#06B8BC"));
		bkPaint.setStyle(Style.FILL);
		bkPaint.setAntiAlias(true);
		
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setStyle(Style.FILL);
		paint.setAntiAlias(true);
		
		boderPaint = new Paint();
		boderPaint.setColor(Color.WHITE);
		boderPaint.setStrokeWidth(2);
		boderPaint.setStyle(Style.STROKE);
		boderPaint.setAntiAlias(true);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		measureWidth = measureWidth(widthMeasureSpec);
		int measureHeight = measureHeight(heightMeasureSpec);
		
		pbHeight = ((float)measureHeight)/5;
		top = 2*pbHeight;
		bottom = pbHeight+top;
		unitSize = (measureWidth-21*dividerHeight)/20;
		left = dividerHeight;
		right = unitSize+left;
		setMeasuredDimension(measureWidth, measureHeight);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		//绘制
		canvas.drawRect(1, top-dividerHeight, measureWidth-1, bottom+dividerHeight, boderPaint);
		for(int i =0;i<20;i++){
			if(i<value/5){
				canvas.drawRect(left, top, right, bottom, paint);
			}else{
				canvas.drawRect(left, top, right, bottom, bkPaint);
			}
			left = left+unitSize+dividerHeight;
			right = left+unitSize;
		}
		//偶先绘制两次的情况，重置起始值避免
		resetData();
	}
	private void resetData() {
		left = dividerHeight;
		right = unitSize + left;		
	}
	private int value;
	public void setProgress(int value){
		this.value = value;
		if(this.value<=30){
			paint.setColor(Color.RED);
		}else{
			paint.setColor(Color.WHITE);
		}
		resetData();
		this.postInvalidate();
	}
	
	private int measureWidth(int pWidthMeasureSpec) {  
		int result = 0;  
		
		int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);  
		int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);  
		
		switch (widthMode) {  
		case MeasureSpec.AT_MOST:  
		case MeasureSpec.EXACTLY:  
			result = widthSize;  
			break;  
		}  
		return result;  
	}  
	private int measureHeight(int pHeightMeasureSpec) {  
		int result = 0;  
		
		int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);  
		int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);  
		
		switch (heightMode) {  
		case MeasureSpec.AT_MOST:  
		case MeasureSpec.EXACTLY:  
			result = heightSize;  
			break;  
		}  
		return result;  
	} 
	
}
